/*
 * Command analyse function for client requests
 * Info:
 * This c file is used for analyzing commands and check what should happen.
 * The original c file was written by Roy Stegers and Gino Biervliet.
 * Edited by Kevin Ouwens for FSP usage, with clientthreads.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "hfile.h"

int clientCmd(int clientNo, clientInfo client[])
{
    printf("clientCmd reached\n");
    client[clientNo].iCount = 0, client[clientNo].i = 0, client[clientNo].n = 0;
    bzero(client[clientNo].command, CMDSIZE);
    bzero(client[clientNo].filename, ARGSIZE);

    printf("dataBuffer: %s\n", client[clientNo].tempBuff);
    printf("passed, length = %d\n", strlen(client[clientNo].tempBuff));
    //substract the command and arguments, because we can and should
    for(client[clientNo].iCount=0;client[clientNo].iCount<2;client[clientNo].iCount++)
    {
        if(client[clientNo].iCount!=1)
        {
            for(client[clientNo].n=0;client[clientNo].tempBuff[client[clientNo].i] != ' ';client[clientNo].n++)
            {
                //printf("n:%d\ni:%d\niCount:%d\n", n, i, iCount);
                client[clientNo].command[client[clientNo].n] = client[clientNo].tempBuff[client[clientNo].i];
                client[clientNo].i++;
            }
            client[clientNo].i++;
            client[clientNo].command[client[clientNo].i] = '\0';
        }
        else
        {
            for(client[clientNo].n=0;client[clientNo].tempBuff[client[clientNo].i] != '\0';client[clientNo].n++)
            {
                //printf("n:%d\ni:%d\niCount:%d\n", n, i, iCount);
                client[clientNo].filename[client[clientNo].n] = client[clientNo].tempBuff[client[clientNo].i];
                client[clientNo].i++;
            }
            client[clientNo].i-2;
            client[clientNo].filename[client[clientNo].i] = '\0';
            break;
        }
    }
    //end of substraction
    printf("command: %s\n", client[clientNo].command);
    printf("file: %s\n", client[clientNo].filename);

    //compare the command to whatever to see what should be done.
    if(strcmp(client[clientNo].command,"QUIT") == 0)
    {
        printf("Client stopping\n");
        close(client[clientNo].iMapFd);
        //need to figure out a nice way to clean out the struct of this client
        //maybe with a function? (should do this later)
    }

    else if(strcmp(client[clientNo].command,"UPLOAD") == 0)
    {
         //do something with upload.c
        printf("UPLOAD function called, clientNo: %d\n", clientNo);
        printf("requested file: %s\n", client[clientNo].filename);
        //handle the upload request
        handleUpload(client[clientNo].clientNo, client);
        printf("going to return UPLOAD\n");
        return 0;
    }

    else if(strcmp(client[clientNo].command, "DOWNLOAD") == 0)
    {
        //do something with download.c
        printf("DOWNLOAD function called, clientNo: %d\n", clientNo);
        //handle the download request
        handleDownload(client[clientNo].clientNo, client);
        return 0;
    }
    else
    {
        printf("invalid command!\n");
        return 0;
    }
    //end of comparison
    return 0;
}